//
//  FormulateTableViewCell.m
//  littleLight's Project-A
//
//  Created by dllo on 16/5/21.
//  Copyright © 2016年 light. All rights reserved.
//

#import "FormulateTableViewCell.h"
#import <UIImageView+WebCache.h>
@interface FormulateTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end
@implementation FormulateTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
        
    }
    return self;
}

- (void)createView
{
    self.bigImageV = [[UIImageView alloc] init];
   // self.bigImageV.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:self.bigImageV];
    self.bigImageV.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.bigImageV addGestureRecognizer:tap];
    
    
    
    self.title = [[UILabel alloc] init];
    self.title.font = [UIFont systemFontOfSize:35];
    self.title.font = [UIFont boldSystemFontOfSize:30];
    self.title.textColor = [UIColor whiteColor];
    self.title.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.title];
    
    self.sub_title = [[UILabel alloc] init];
    self.sub_title.font = [UIFont boldSystemFontOfSize:17];
    self.sub_title.textAlignment = NSTextAlignmentCenter;
    self.sub_title.textColor = [UIColor whiteColor];
    [self.contentView addSubview:self.sub_title];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize  = CGSizeMake(self.contentView.frame.size.width / 2 - 10 , 250);
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
    layout.minimumInteritemSpacing = 10;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.myCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.contentView.frame.size.height / 2, self.contentView.frame.size.width, 250) collectionViewLayout:layout];
    self.myCollectionV.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.myCollectionV];
    
    self.myCollectionV.delegate = self;
    self.myCollectionV.dataSource = self;
    self.myCollectionV.showsHorizontalScrollIndicator = NO;
    self.myCollectionV.bounces = NO;
    [self.myCollectionV registerClass:[FormulateCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.bigImageV.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height / 2);
    self.title.frame = CGRectMake(20, self.bigImageV.frame.size.height / 5  , self.contentView.frame.size.width - 40, 60);
    self.sub_title.frame = CGRectMake(20, self.bigImageV.frame.size.height / 5  + 50, self.contentView.frame.size.width - 40, 40);
    
    
     self.myCollectionV.frame = CGRectMake(0, self.contentView.frame.size.height / 2, self.contentView.frame.size.width, 270);
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
  //  NSLog(@"%@",self.model);
    self.myBlock(self.model);
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   // NSLog(@"%ld %s", self.array.count, __PRETTY_FUNCTION__);
    return self.array.count;
}

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FormulateListBateModel *model = [self.array objectAtIndex:indexPath.row];
    self.youBlock(model);
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FormulateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    FormulateListBateModel *model = [self.array objectAtIndex:indexPath.row];
    cell.model = model;
    //[self.myCollectionV reloadData];
    
    //cell.backgroundColor = [UIColor magentaColor];
    return cell;
    
}

- (void)setModel:(FormulateBaseModel *)model
{
    _model = model;
    [self.bigImageV sd_setImageWithURL:[NSURL URLWithString:_model.cover_image_url]];
    self.title.text = _model.title;
    self.sub_title.text = _model.sub_title;
    
    
}

- (void)setArray:(NSMutableArray *)array
{
    _array = array;
    [self.myCollectionV reloadData];
}

@end
